package com.android.place360.geocoding;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.android.place360.R;
import com.android.place360.appsettings.SharePreference_Setting;
import com.android.place360.current_location.MyLocationManager;

public class FindingLocation extends Activity {
	// private final String tag = "place360";
	private List<String> listAutoComplete;
	private EditText txtSeachKey;
	private ArrayAdapter<String> adapter;
	ListView listView;
	Address selectedLocation = null;
	String api_key;
	String language;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_finding_location);

		api_key = getString(R.string.google_place_api_key);
		language = SharePreference_Setting.getLanguage(getApplicationContext());

		listView = (ListView) findViewById(R.id.finding_location_list);
		txtSeachKey = (EditText) findViewById(R.id.layout_findding_location_txtKey);
		listView.setOnItemClickListener(listItemClick);

		TextView txtCurrentAddress = (TextView) findViewById(R.id.layout_findding_location_tvCurrentAddress);
		
		txtCurrentAddress.setText(MyLocationManager.getCurrentAddressString());

		// thread to do search location during text change
		new Thread(new Runnable() {

			@Override
			public void run() {
				txtSeachKey.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						handler.removeMessages(1);
						handler.sendEmptyMessageDelayed(1, 1000);
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
					}

					@Override
					public void afterTextChanged(Editable s) {

					}
				});
			}
		}).start();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if (txtSeachKey.getText().toString().trim().equals(""))
					break;
				listAutoComplete = GooglePlaceAutoComplete.getGPAPlaces(
						txtSeachKey.getText().toString().trim(), api_key,
						language);
				if (listAutoComplete == null) {
					listAutoComplete = new ArrayList<String>();
				}
				adapter = new CustomListAdapterLocation(
						getApplicationContext(),
						R.layout.custom_list_find_location,
						(ArrayList<String>) listAutoComplete);
				listView.setAdapter(adapter);

				break;
			}
		}
	};

	OnItemClickListener listItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			final Geocoder fwdGeocoder = new Geocoder(getApplicationContext(),
					new Locale(language));
			List<Address> locations = null;
			try {
				locations = fwdGeocoder.getFromLocationName(listAutoComplete
						.get(position), 1);
			} catch (IOException e) {

				e.printStackTrace();
			}
			if (locations != null && !locations.isEmpty()) {
				selectedLocation = locations.get(0);
				ShowConfirm();
			}
		}
	};

	private void ShowConfirm() {
		new AlertDialog.Builder(this).setIcon(
				android.R.drawable.ic_dialog_alert).setTitle(
				R.string.change_locaiton_title).setMessage(
				R.string.change_location).setPositiveButton(
				R.string.confirm_remove_favourite_btnYes,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						MyLocationManager.setLocation(selectedLocation);
						setResult(RESULT_OK, getIntent());
						finish();
					}
				}).setNegativeButton(R.string.confirm_remove_favourite_btnNo,
				null).show();
	}
}
